package com.joolun.web.controller.script;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.core.domain.R;
import com.joolun.script.entity.WxUserExt;
import com.joolun.script.service.WxUserExtService;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 用户扩展表
 *
 * @author 臣服
 * @date 2022-01-12 10:52:45
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxuserext")
@Api(value = "wxuserext", tags = "用户扩展表管理")
public class WxUserExtController {

    private final WxUserExtService wxUserExtService;

    /**
     * 分页列表
     *
     * @param page      分页对象
     * @param wxUserExt 用户扩展表
     * @return
     */
    @ApiOperation(value = "分页列表")
//    @GetMapping("/page")
//    @PreAuthorize("@ss.hasPermi('weixin:wxuserext:index')")
    public R<Page<WxUserExt>> getPage(Page page, WxUserExt wxUserExt) {
        return R.ok(wxUserExtService.page(page, Wrappers.query(wxUserExt)));
    }

    /**
     * 用户扩展表查询
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "用户扩展表查询")
    @GetMapping("/{id}")
    public R<WxUserExt> getById(@PathVariable("id") String id) {
        return R.ok(wxUserExtService.getById(id));
    }

    /**
     * 用户扩展表新增
     *
     * @param wxUserExt 用户扩展表
     * @return R
     */
    @ApiOperation(value = "用户扩展表新增")
//    @PostMapping
//    @PreAuthorize("@ss.hasPermi('weixin:wxuserext:add')")
    public R<Boolean> save(@RequestBody WxUserExt wxUserExt) {
        return R.ok(wxUserExtService.save(wxUserExt));
    }

    /**
     * 用户扩展表修改
     *
     * @param wxUserExt 用户扩展表
     * @return R
     */
    @ApiOperation(value = "用户扩展表修改 | 修改学校")
    @PutMapping
    public R<Boolean> updateById(@RequestBody WxUserExt wxUserExt) {
        return R.ok(wxUserExtService.updateById(wxUserExt));
    }

    /**
     * 用户扩展表删除
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "用户扩展表删除")
//    @DeleteMapping("/{id}")
//    @PreAuthorize("@ss.hasPermi('weixin:wxuserext:del')")
    public R removeById(@PathVariable String id) {
        String userId = ThirdSessionHolder.getThirdSession().getWxUserId();
        WxUserExt ext = wxUserExtService.getById(id);
        if (Objects.isNull(ext)) {
            return R.ok("删除成功");
        }
        synchronized (this) {
            if (Objects.isNull(ext)) {
                return R.ok("删除成功");
            }
            Boolean isAdmin = ThirdSessionHolder.getThirdSession().getIsAdmin();
            if (userId.equals(ext.getUserId()) || isAdmin) {
                return R.ok(wxUserExtService.removeById(id));
            }
        }
        return R.ok("已删除");
    }

}
